<!--  -->

<style lang='scss' type='stylesheet/scss' scoped>
.file-item {
  display: flex;
  padding: 10px;
  padding-right: 0;
  cursor: pointer;
  &.active {
    background: rgba(51, 173, 51, 0.18) !important;
  }
  &:hover {
    background: rgba(51, 173, 51, 0.18) !important;
  }
  &.disabled {
    cursor: not-allowed;
    opacity: 0.5;
  }
  i {
    flex: 0 0 24px;
    font-size: 16px;
    line-height: 30px;
    margin-right: 5px;
  }
  .file-icon {
    flex: 0 0 30px;
    width: 30px;
    height: 30px;
    margin-right: 20px;
  }
  .file-name {
    color: #535353;
    flex: 1;
    line-height: 30px;
    font-size: 12px;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>

<template>
  <div>
    <div class="file-item"
         :class="`${activedFile.id===file.id?'active':''} ${checkFiles.includes(file.id)&&file.isFolder?'disabled':'enabled'}`"
         :style="`padding-left:${index*20}px`"
         @click="trigglerItemOpen">
      <i :class="`${fileIcon}`"
         style="text-align:center"></i>
      <img :src="require(`../../assets/icons/${file.isFolder?'folder':file.type.split('/')[0]}.png`)"
           class="file-icon">
      <div class="file-name">{{file.name}}</div>
    </div>
    <div class="file-item"
         v-if="newFolderVisible&&+activedFile.id === +file.id"
         :style="`padding-left:${(index+1)*20}px`"
         @click="trigglerItemOpen">
      <i class="el-icon-arrow-right"
         style="opacity:0"></i>
      <img src="../../assets/icons/folder.png"
           class="file-icon">
      <input type="text"
             ref="fileName"
             @keydown.enter="blurInput"
             @blur="inputBlur"
             v-model="fileName"
             style="width:100%;outline:none"
             class="file-name">
    </div>
    <file-item v-if="file.children&&file.children.length&&file.open"
               @hideInput="hideInput"
               @updateFileList="updateFileList"
               :activedFile="activedFile"
               :newFolderVisible.sync="newFolderVisible"
               v-for="item in file.children"
               :index="index+1"
               :key="item.id"
               @checkFile="checkFile"
               :file="item"></file-item>
  </div>
</template>

<script type='text/ecmascript-6'>
import { mapGetters } from "vuex";
export default {
  name: "file-item",
  props: ["file", "index", "activedFile", "newFolderVisible"],
  data() {
    return {
      fileName: ""
    };
  },
  computed: {
    ...mapGetters(["checkFiles"]),
    fileIcon() {
      let file = this.file;
      if (this.checkFiles.includes(file.id)) {
        return "";
      }
      if (!file.isFolder) {
        return "";
      }
      if (file.isLoad && !file.children.length) {
        return "";
      }
      if (file.isLoading) {
        return "el-icon-loading";
      }
      return file.open ? "el-icon-arrow-down" : "el-icon-arrow-right";
    }
  },
  watch: {
    newFolderVisible(newVal) {
      this.$nextTick(_ => {
        if (newVal && this.$refs.fileName) {
          this.$refs.fileName.focus();
        }
      });
    }
  },
  methods: {
    blurInput() {
      this.$refs.fileName.blur();
    },
    hideInput() {
      this.$emit("hideInput");
    },
    updateFileList(file, res) {
      this.$emit("updateFileList", file, res);
    },
    async inputBlur() {
      let fileName = this.fileName.trim();
      if (fileName) {
        let res = await this.$store.dispatch("file/boxAddFolder", {
          folderName: fileName,
          pid: this.activedFile.id
        });

        this.fileName = "";
        this.$emit("updateFileList", this.file, res);
      }
      this.$emit("hideInput");
    },
    checkFile(file) {
      if (!this.checkFiles.includes(file.id) && file.isFolder) {
        this.$emit("checkFile", file);
      } else if (file.isFolder) {
        this.$notify.warning({
          title: "警告",
          message: "不能移动或复制到选中文件夹目录下"
        });
      }
    },
    trigglerItemOpen() {
      this.$emit("checkFile", this.file);
    }
  }
};
</script>
